package com.ridbor001.demo.dao;

import com.ridbor001.demo.bean.RDBTeacher;
import com.ridbor001.demo.util.RDBDataStore;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

public class RDBTeacherDAO {
    RDBDataStore dataStore = new RDBDataStore();
    // 检查教师是否存在
    public   boolean teacherExists(String teacherId) throws SQLException {
        String sql = "SELECT COUNT(*) FROM teachers WHERE teacher_id = ?";

        try (Connection conn = dataStore.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {

            pstmt.setString(1, teacherId);
            try (ResultSet rs = pstmt.executeQuery()) {
                if (rs.next()) {
                    return rs.getInt(1) > 0;
                }
            }
        }
        return false;
    }
    // 添加教师
    public   void addTeacher(RDBTeacher teacher) throws SQLException {
        String sql = "INSERT INTO teachers (" +
                "teacher_id, name, password, gender, age, phone" +
                ") VALUES (?, ?, ?, ?, ?, ?)";

        try (Connection conn = dataStore.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {

            pstmt.setString(1, teacher.getTeacherId());
            pstmt.setString(2, teacher.getName());
            pstmt.setString(3, teacher.getPassword());
            pstmt.setString(4, teacher.getGender());
            pstmt.setInt(5, teacher.getAge());
            pstmt.setString(6,teacher.getPhone());
            pstmt.executeUpdate();
        }
    }
    // 根据教师ID和密码查找教师
    public Optional<RDBTeacher> findTeacherByIdAndPassword(String teacherId, String password)
            throws SQLException {

        String sql = "SELECT * FROM teachers WHERE teacher_id = ? AND password = ?";

        try (Connection conn = dataStore.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {

            pstmt.setString(1, teacherId);
            pstmt.setString(2, password);

            try (ResultSet rs = pstmt.executeQuery()) {
                if (rs.next()) {
                    return Optional.of(new RDBTeacher(
                            rs.getString("name"),
                            rs.getString("password"),
                            rs.getString("gender"),
                            rs.getInt("age"),
                            rs.getString("phone"),
                            rs.getString("teacher_Id")
                    ));
                }
            }
        }
        return Optional.empty();
    }
    public   RDBTeacher findTeacherById(String teacherId) throws SQLException {
        final String sql = "SELECT teacher_id, name, password, gender, age, phone "
                + "FROM teachers WHERE teacher_id = ?";

        try (Connection conn = dataStore.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, teacherId);
            try (ResultSet rs = pstmt.executeQuery()) {
                if (rs.next()) {
                    return new RDBTeacher(
                            rs.getString("name"),
                            rs.getString("password"),
                            rs.getString("gender"),
                            rs.getInt("age"),
                            rs.getString("phone"),
                            rs.getString("teacher_id")
                    );
                }
            }
        }
        return null;
    }

    // 获取所有学生
    public List<RDBTeacher> getAllTeachers() throws SQLException {
        String sql = "SELECT * FROM teachers";
        List<RDBTeacher> teachers = new ArrayList<>();

        try (Connection conn = dataStore.getConnection();
             Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery(sql)) {

            while (rs.next()) {
                teachers.add(new RDBTeacher(
                        rs.getString("name"),
                        rs.getString("password"),
                        rs.getString("gender"),
                        rs.getInt("age"),
                        rs.getString("phone"),
                        rs.getString("teacher_id")
                ));
            }
        }
        return teachers;
    }
}
